// components/lostshow/lostshow.js
const app = getApp();
const db = wx.cloud.database();
var sev = require('../../utils/sev.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的初始数据
   */
  data: {
    map: {
      latitude: 0,
      longitude: 0,
      markers: [],
      hasMarkers: false//解决方案
    },
    modalName: null,
    img: '',
    mpdata: null,
    content: '',
    imglist: [],
    remarks: [],
    nm: true,
    text: '',
    doc: '',
    modalhide: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    ViewImage(e) {
      console.log(e)
      wx.previewImage({
        urls: this.data.imglist,
        current: e.currentTarget.dataset.url
      });
    },
    toconnect() {
      var that = this;
      wx.makePhoneCall({
        phoneNumber: that.data.calls,
      })
    },
    toup() {
      var that = this;
      that.hideModal();
      wx.showLoading({
        title: '评论上传中',
      })
      wx.cloud.callFunction({
        name: 'msgcheck',
        data: {
          content: that.data.text
        }
      }).then(ckres => {
        if (ckres.result.errCode == 0) {
          wx.cloud.callFunction({
            name: 'kindb',
            data: {
              tablename: 'remarks',
              opr: 'add',
              data: {
                remarker: app.eduOS.xh,
                isNm: that.data.nm,
                date: sev.formatTime(new Date()),
                belongs: that.data.doc,
                text: that.data.text
              }
            },
            success(res) {
              console.log(res)
              wx.hideLoading();
              db.collection('remarks').skip(that.data.remarks.length).orderBy('date', 'asc').where({
                belongs: that.data.doc
              }).get({
                success(ans) {

                  console.log(ans)
                  that.setData({
                    text: '',
                    remarks: that.data.remarks.concat(ans.data)
                  })
                },
                fail(ans) {
                  console.log(ans)
                }
              })
            }
          })
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '提醒',
            content: '请注意言论',
            showCancel: false
          })
        }
      })

      /*
      
      */
    },
    change() {
      this.setData({
        nm: !this.data.nm
      }, () => {
        console.log(this.data.nm)
      })
    },
    textareaAInput(e) {
      this.setData({
        text: e.detail.value
      })
    },
    toremark() {
      if (app.eduOS.xh == '') {
        wx.showModal({
          title: '无权限',
          content: '',
          showCancel: false
        })
      } else {
        this.setData({
          modalhide: false,
          modalName: 'DialogModal1'
        })
      }

    },

    hideModal(e) {
      this.setData({

        modalName: null,
        modalhide: true
      })
    }
  },
  lifetimes: {
    created() {
      var that = this;

      wx.getStorage({
        key: 'selbbs',
        success: function (res) {
          var tmap = res.data.mapdata;
          console.log(tmap)
          that.setData({
            content: res.data.content,
            imglist: res.data.realList,
            doc: res.data._id,
            'map.latitude': tmap.latitude,
            'map.longitude': tmap.longitude,
            'map.markers': [{
              latitude: tmap.latitude,
              longitude: tmap.longitude,
              name: tmap.name,
              desc: tmap.address,
              iconPath: res.data.realList[0],
              width: 150,
              height:150
            }],
            'map.hasMarkers': true//解决方案
            
            
          }, () => {
            db.collection('remarks').skip(that.data.remarks.length).orderBy('date', 'asc').where({
              belongs: that.data.doc
            }).get({
              success(ans) {
                console.log(ans)
                that.setData({
                  remarks: ans.data
                })
              },
              fail(ans) {
                console.log(ans)
              }
            })
          })
        }
      })

    }
  }
})
